package cn.itsource.org.service.impl;

import cn.itsource.base.service.impl.BaseServiceImpl;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import cn.itsource.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        //如果上级部门为空  顶级部门 dirpath 就是 /自己的id
        if(department.getParent()==null){
            String dpath="/"+department.getId();
            department.setDirPath(dpath);
        }else {
            //如果上级部门不为空  dirpath 获取上级部门的dirpath     /上级部门/自己的id
            Department parent = this.queryById(department.getParent().getId());
            department.setDirPath(parent.getDirPath()+"/"+department.getId());
        }
        update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        if(department.getParent()==null){
            String dpath="/"+department.getId();
            department.setDirPath(dpath);
        }else {
            //如果上级部门不为空  dirpath 获取上级部门的dirpath     /上级部门/自己的id
            Department parent = this.queryById(department.getParent().getId());
            department.setDirPath(parent.getDirPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }





    @Override
    public void patchDel(Long[] ids) {
        departmentMapper.patchDel(ids);
    }

    @Override
    public List<Department> getDeptTree() {
        return departmentMapper.getDeptTree();
    }
}
